package cn.com.tctec.faked;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jiangz
 *
 */
public class CommonDatum {

    /**
     * 对应一条统计数据
     */
    private String tagF;
    private String measurementName;
    private Double deviation;
    private Long timeInSecond;

    public CommonDatum(String tagF, String measurementName, Double deviation,
            Long timeInSecond) {
        this.tagF = tagF;
        this.measurementName = measurementName;
        this.deviation = deviation;
        this.timeInSecond = timeInSecond;
    }
    
    public String geneInsertString(DBEnum dbEnum) {
        switch(dbEnum) {
            case INFLUX_DB:
                return this.geneInfluxDBInsertString();
            case MS_SQL:
                return this.geneSqlServerInsertString();
        }
        return null;
    }

    private String geneInfluxDBInsertString() {
        return String.format(
                "%s,%s=%s %s=%f %d",
                this.measurementName,
                Constant.InfluxDB.HIS_MEASUREMENT_TAG_KEY, this.tagF,
                Constant.Statistic.DEVIATION_VALUE_FIELD_NAME, this.deviation,
                this.timeInSecond * 1000 * 1000 * 1000);
    }
    
    private String geneSqlServerInsertString() {
        return String.format(
                "INSERT INTO [%s] ([tagId], [gatherTime], [deviation]) VALUES ('%s', '%s', %f);",
                this.measurementName,
                this.tagF,
                new SimpleDateFormat(Constant.DATETIME_FMT).format(new Date(this.timeInSecond * 1000)),
                this.deviation);
    }

    public Double getDeviation() {
        return this.deviation;
    }

    @Override
    public String toString() {
        return String.format("%-25s%-20s%s%16f",
                this.measurementName, this.tagF,
                new SimpleDateFormat(Constant.DATETIME_FMT).format(new Date(timeInSecond * 1000)),
                this.deviation);
    }
}
